package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.PageInfo;
import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysDictTypeQueryDTO;
import cn.com.dcsgo.domain.dto.SysDictTypeSaveDTO;
import cn.com.dcsgo.domain.dto.SysDictTypeUpdateDTO;
import cn.com.dcsgo.domain.po.SysDictType;
import cn.com.dcsgo.enums.ResultEnums;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysDictTypeService;
import cn.com.dcsgo.utils.DictDataLocalCache;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * SysDictTypeController
 *
 * @author Dcsgo
 * @since 23/5/2025 上午 9:16
 */
@Tag(name = "字典类型")
@RestController
@RequestMapping("sysDictType")
@RequiredArgsConstructor
public class SysDictTypeController extends BaseController {
    private final SysDictTypeService sysDictTypeService;
    private final DictDataLocalCache dictDataLocalCache;

    @MyPermission({"system:dictType:query"})
    @Operation(summary = "分页条件查询字典类型")
    @PostMapping("getByPage")
    public Result<PageInfo<SysDictType, SysDictTypeQueryDTO>> getPage(@RequestBody PageInfo<SysDictType, SysDictTypeQueryDTO> pageInfo) {
        PageInfo<SysDictType, SysDictTypeQueryDTO> result = sysDictTypeService.page(pageInfo);
        return new Result<>(ResultEnums.SUCCESS, result);
    }

    @MyPermission({"system:dictType:query"})
    @Operation(summary = "根据字典编号获取字典类型")
    @GetMapping("get/{dictId}")
    public Result<SysDictType> getDictType(@PathVariable Long dictId) {
        return new Result<>(sysDictTypeService.getDictTypeByDictId(dictId));
    }

    @MyPermission({"system:dictType:add"})
    @Operation(summary = "添加字典类型")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysDictTypeSaveDTO sysDictTypeSaveDTO) {
        int row = sysDictTypeService.save(sysDictTypeSaveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dictType:update"})
    @Operation(summary = "修改字典类型")
    @PutMapping("update")
    public Result<Serializable> update(@Validated @RequestBody SysDictTypeUpdateDTO sysDictTypeUpdateDTO) {
        int row = sysDictTypeService.updateById(sysDictTypeUpdateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dictType:remove"})
    @Operation(summary = "删除字典类型")
    @DeleteMapping("delete/{dictId}")
    public Result<Serializable> delete(@PathVariable Long dictId) {
        int row = sysDictTypeService.remove(dictId);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dictType:cache"})
    @Operation(summary = "同步字典数据到缓存")
    @GetMapping("refreshCache")
    public Result<Serializable> refreshCache() {
        dictDataLocalCache.refresh();
        return new Result<>(ResultEnums.SUCCESS);
    }
}
